
package com.GPS_based_training.JSONUtils;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.nio.MappedByteBuffer;
import java.nio.channels.FileChannel;
import java.nio.charset.Charset;

import org.json.JSONException;
import org.json.JSONObject;

import android.os.Environment;




/**
 * In this class the operations between files will be managed. 
 * and JSONObjects 
 * @author Pietro Meloni & Danilo Dessi'
 * 
 */
public class JSONFileManager {
	
	//extension of the file that will be saved
	public static final String EXT = ".json";
	
	
	public static final String PATH = Environment.getExternalStorageDirectory() + "/Android/data/com.GPS-Based-Training.GPS-Based-Training/file";
	
	/**
	 * @author Pietro Meloni
	 * 
	 * Save a JSONObject in a file with json extension
	 * @param actualTraining the JSONObject that must be saved
	 * @param fileName the name of the file that will be created
	 * @throws IOException for the writing of the file
	 */
	public static void saveOnFile (JSONObject actualTraining, String fileName) throws IOException {
		
		//Create the File for writing the a file in the correct directory
		File directoryForTheProject = new File(PATH);
		
		//if the directory doesn't exist, it will be created
		if( !directoryForTheProject.exists() ) {
			
			directoryForTheProject.mkdirs();
			
		}
		
		//create a File object with path desidered
		File jsonFile = new File(PATH + "/" + fileName + EXT);
		
		FileWriter fileWriterForJSON = new FileWriter(jsonFile);
		jsonFile.createNewFile();
		
		//trying to save the Training converted in JSONObject in a file
		try {
			
			fileWriterForJSON.write(actualTraining.toString());
			fileWriterForJSON.flush();
			
		}catch(IOException exception) {
			
			exception.printStackTrace();
			
		}
		finally{
			
			fileWriterForJSON.close();
			
		}
		
	}
	
	/**
	 * @author Pietro Meloni
	 * 
	 * Reads a Training converted in JSONObject from a file
	 * @param fileName the name of the file that will be read
	 * @return the JSONObject contained in the file
	 * @throws IOException
	 */
	public static JSONObject readFromFile(File jsonFile ) throws IOException {
		
		//initialising variables that will be user later
		FileInputStream jsonReader = null;
		String jsonString = null;
		FileChannel fileChanneled = null;
		JSONObject objectReaded = null;
		
		//trying to read the file and putting it in a JSONObject
		try {
	
			//reading the file
			jsonReader = new FileInputStream(jsonFile);
			fileChanneled = jsonReader.getChannel();
            MappedByteBuffer byteBuffer = fileChanneled.map(FileChannel.MapMode.READ_ONLY, 0, fileChanneled.size());
            jsonString = Charset.defaultCharset().decode(byteBuffer).toString();
            
            //putting in a JSONObject
            try{
            	
            	objectReaded = new JSONObject(jsonString);
            	
            } catch (JSONException exception) {
            	
            	exception.printStackTrace();
            	
            }
            
		
		} catch (IOException exception) {
			
			exception.printStackTrace();
			
		} finally {
			
			//resources are saved
			fileChanneled.close();
			jsonReader.close();
			
		}
		
		
		
		return objectReaded;
	}
	
	/**
	 * @author Danilo Dessi'
	 * This method is used to delete all file that are created by one activity
	 */
	public static void deleteAllFile() {
		
		File directory = new File(PATH);
		
		if( directory != null ){
			
			//Retrieval all file of the directory
			File[] files = directory.listFiles();
			
			if(files != null) {
				
				//Deleting files
				for (File f : files) {
					f.delete();
				}
				
			}
		}
	}
	
	
	
	
	
	
	
	

}
